package cn.shengchao.examstar.knowledge.acl.adapters.repositories;

import cn.shengchao.examstar.knowledge.acl.ports.repositories.IKnowledgeQuestionRepository;
import cn.shengchao.examstar.knowledge.infrastructure.dao.TKnowledgeQuestionMapper;
import cn.shengchao.examstar.knowledge.infrastructure.dao.po.TKnowledgeQuestion;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class KnowledgeQuestionRepository implements IKnowledgeQuestionRepository {

    @Autowired
    private TKnowledgeQuestionMapper knowledgeQuestionMapper;


    @Override
    public void save(TKnowledgeQuestion tKnowledgeQuestion) {
        TKnowledgeQuestion knowledgeQuestion = knowledgeQuestionMapper.selectOne(Wrappers.lambdaQuery(TKnowledgeQuestion.class).eq(TKnowledgeQuestion::getKnowledgeId, tKnowledgeQuestion.getKnowledgeId())
                .eq(TKnowledgeQuestion::getQuestionId, tKnowledgeQuestion.getQuestionId()));
        if (knowledgeQuestion == null) {
            knowledgeQuestionMapper.insert(tKnowledgeQuestion);
        }
    }

    @Override
    public void delete(TKnowledgeQuestion tKnowledgeQuestion) {
        knowledgeQuestionMapper.delete(Wrappers.lambdaQuery(TKnowledgeQuestion.class).eq(TKnowledgeQuestion::getQuestionId, tKnowledgeQuestion.getQuestionId()));
    }

    @Override
    public void update(TKnowledgeQuestion tKnowledgeQuestion) {
        TKnowledgeQuestion update = new TKnowledgeQuestion();
        update.setStatus(tKnowledgeQuestion.getStatus());
        knowledgeQuestionMapper.update(update, Wrappers.lambdaUpdate(TKnowledgeQuestion.class)
                .eq(TKnowledgeQuestion::getQuestionId, tKnowledgeQuestion.getQuestionId()));
    }
}
